package com.cennavi.minemapdemo.activity.basic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.activity.BaseActivty;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.DensityUtil;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.camera.CameraUpdateFactory;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minedata.minemap.overlay.MarkerLayer;
import com.minedata.minemap.overlay.MarkerLayerOptions;

/**
 * Created by lijingru on 2018/12/17
 */
public class LocalDeployMapActivity extends BaseActivty {
    private MapView mapView;
    private MineMap mineMap;
    private Context mContext;
    private MarkerLayerOptions.MarkerItem marker; // 定位的标记点
    private MarkerLayer locationMarkerLayer;
//    http://www.minedata.cn/operate/view?solution=8921&accessToken=8e2fbb4a7c9a4ca2be057671cb8d7588
//    http://www.minedata.cn/operate/view?solution=8921&accessToken=8e2fbb4a7c9a4ca2be057671cb8d7588
//    //云南项目环境（负载均衡）
//	public static final String MAP_TOK = "6ed83a09b32547bb975102422a453d6c";
//	public static final String MAP_ID = "3589";
//	public static final String mBaseUrl = "http://10.166.190.228/service/solu/style/id/";

    //沈阳测试服务器
//	http://59.151.82.197:18199/operate/view?solution=3479&accessToken=6ff57d5c121f4aa99943e5d42b306f98
    public static final String MAP_TOK = "6ff57d5c121f4aa99943e5d42b306f98";
    public static final String MAP_ID = "3479";//5699
    public static final String mBaseUrl = "http://59.151.82.197:18199/service/solu/style/id/";
    public static final String OFFLINE_TOK = "6ff57d5c121f4aa99943e5d42b306f98";



    public static LatLng Kunming = new LatLng(24.98700,102.69474);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        MinemapAccountManager.getInstance(this, MAP_TOK, MAP_TOK, MAP_ID);
        setContentView(R.layout.activity_map_localdeploy);
        mapView = findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        //初始化地图
        initMap();

    }


    private void initMap() {

        //mine map sdk 2.0.6之前生效
        // mapView.setStyleUrl(Config.mBaseUrl + Config.MAP_ID);

        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
                mineMap.getUiSettings().setZoomControlsEnabled(false);
                mineMap.getUiSettings().setRotateGesturesEnabled(true);// 是否可以旋转地图
                mineMap.getUiSettings().setTiltGesturesEnabled(true);
                mineMap.setStyleUrl(Config.mBaseUrl + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {
                    @Override
                    public void onStyleLoaded(String s) {

                        //mine map sdk 2.0.6及之后版本这样使用
                        addImageToMap();

                    }
                });
                //设置指南针是否显示，默认是显示
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.getUiSettings().setCompassGravity(Gravity.LEFT);
                mineMap.getUiSettings().setCompassMargins(DensityUtil.dip2px(mContext, 11), DensityUtil.dip2px(mContext, 80),
                        0, 0);
                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(Kunming).zoom(14)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                mineMap.setMinZoomPreference(3);//设置最低的展示级别

                marker = new MarkerLayerOptions.MarkerItem().icon(R.drawable.ic_arrow+"").position(Kunming);
                MarkerLayerOptions Layer = new MarkerLayerOptions("locationMarkerLayer")
                        .iconAnchor(MarkerLayerOptions.ANCHOR_CENTER)//基准点
                        .iconPitchAlignment(MarkerLayerOptions.PITCH_ALIGNMENT_MAP);
                Layer.add(marker);
                locationMarkerLayer = mineMap.addLayer(Layer);
            }
        });
    }
    //加载可能的图片资源
    private void addImageToMap() {

        Bitmap icon = drawableToBitmap(this, R.drawable.ic_arrow);
        mineMap.addImage(R.drawable.ic_arrow + "", icon);

    }
    /**
     * drawable 需要用这个方法转成 Bitmap
     * @param context
     * @param vectorDrawableId
     * @return
     */
    public static Bitmap drawableToBitmap(Context context, int vectorDrawableId) {
        Bitmap bitmap = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            Drawable vectorDrawable = context.getDrawable(vectorDrawableId);
            if (vectorDrawable != null) {
                bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
                        vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_4444);
                Canvas canvas = new Canvas(bitmap);
                vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                vectorDrawable.draw(canvas);
            }
        } else {
            bitmap = BitmapFactory.decodeResource(context.getResources(), vectorDrawableId);
        }
        return bitmap;
    }

    public void zoomIn(View view) {
        if (mineMap != null) {
            mineMap.moveCamera(CameraUpdateFactory.zoomIn());
        }
    }

    public void zoomOut(View view) {
        if (mineMap != null) {
            mineMap.moveCamera(CameraUpdateFactory.zoomOut());
        }
    }

    //生命周期
    @Override
    protected void onStart() {
        super.onStart();
        if (mapView != null)
            mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null)
            mapView.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //2.0.6及之后版本 需要将加入地图的图片资源进行清理
        if (mineMap != null)
            mineMap.removeImagesAll();


        if (mapView != null)
            mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (mapView != null){
            mapView.onLowMemory();
        }
    }

}
